package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.technology.entity.ProdTecRoutProcTechDrawing;
import com.mxpio.erp.common.technology.entity.ProdTecRoutProcTechDrawingSignIn;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcTechDrawingService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Tag(name = "ProdTecRoutProcTechDrawingController", description = "产品工艺路线明细(工序)工艺图纸")
@RestController
@RequestMapping("/erp/tech/prodrout/proc/drawingfile/")

public class ProdTecRoutProcTechDrawingController {

	@Autowired
	private ProdTecRoutProcTechDrawingService service;

	@GetMapping("list")
	@Operation(summary = "产品工艺路线明细(工序)工艺图纸列表", description = "获取产品工艺路线明细(工序)工艺图纸列表", method = "GET")
	public Result<List<ProdTecRoutProcTechDrawing>> list(Criteria criteria) {
		List<ProdTecRoutProcTechDrawing> itemDesignFiles = service.list(criteria);
		return Result.OK(itemDesignFiles);
	}

	@GetMapping("page")
	@Operation(summary = "产品工艺路线明细(工序)工艺图纸列表", description = "获取产品工艺路线明细(工序)工艺图纸列表", method = "GET")
	public Result<Page<ProdTecRoutProcTechDrawing>> page(Criteria criteria, Integer pageSize, Integer pageNo) {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ProdTecRoutProcTechDrawing> itemDesignFiles = service.listPage(ProdTecRoutProcTechDrawing.class, page, criteria);
		return Result.OK(itemDesignFiles);
	}

	@GetMapping("list_by_proc/{routProcId}/{version}")
	@Operation(summary = "产品工艺路线明细(工序)工艺图纸列表", description = "获取产品工艺路线明细(工序)工艺图纸列表", method = "GET")
	public Result<List<ProdTecRoutProcTechDrawing>> listByItemCode(@PathVariable(name = "routProcId", required = true) String routProcId,
																   @PathVariable(name = "version", required = true) Integer version) {
		Criteria criteria = Criteria.create();
		criteria.addCriterion("routProcId", Operator.EQ,routProcId);
		criteria.addCriterion("version", Operator.EQ,version);
		criteria.addCriterion("primary",Operator.EQ,CommonEnums.YesNo.YES.getCode());
		List<ProdTecRoutProcTechDrawing> itemDesignFiles = service.list(ProdTecRoutProcTechDrawing.class, criteria);
		return Result.OK(itemDesignFiles);
	}
	@PostMapping("add")
	@Operation(summary = "新增产品工艺路线明细(工序)工艺图纸", description = "新增产品工艺路线明细(工序)工艺图纸", method = "POST")
	public Result<ProdTecRoutProcTechDrawing> add(@RequestBody ProdTecRoutProcTechDrawing itemDesignFile) {
		if (StringUtils.isBlank(itemDesignFile.getFileNo())){
			return Result.error("请上传对应文件");
		}
		if (StringUtils.isBlank(itemDesignFile.getRoutProcId())){
			return Result.error("路线工序ID不能为空");
		}
		if (itemDesignFile.getVersion()==null){
			return Result.error("路线版本不能为空");
		}
		service.save(itemDesignFile);//重写
		return Result.OK(itemDesignFile);
	}

	@PostMapping("add_batch")
	@Operation(summary = "新增产品工艺路线明细(工序)工艺图纸（批量）", description = "新增产品工艺路线明细(工序)工艺图纸（批量）", method = "POST")
	public Result<?> add(@RequestBody List<ProdTecRoutProcTechDrawing> itemDesignFiles) {
		if (itemDesignFiles==null || itemDesignFiles.isEmpty()){
			return Result.error("请求参数为空");
		}
		for(ProdTecRoutProcTechDrawing file:itemDesignFiles){
			if (StringUtils.isBlank(file.getFileNo())){
				return Result.error("请上传对应文件");
			}
			if (StringUtils.isBlank(file.getRoutProcId())){
				return Result.error("路线工序ID不能为空");
			}
			if (file.getVersion()==null){
				return Result.error("路线版本不能为空");
			}
			if(StringUtils.isEmpty(file.getFamily())){
				file.setFamily(UUID.randomUUID().toString().replace("-",""));
			}
		}
		service.save(itemDesignFiles);//重写
		return Result.OK();
	}

	@PutMapping("edit")
	@Operation(summary = "编辑产品工艺路线明细(工序)工艺图纸", description = "编辑产品工艺路线明细(工序)工艺图纸（全量）", method = "PUT")
	public Result<ProdTecRoutProcTechDrawing> edit(@RequestBody ProdTecRoutProcTechDrawing itemDesignFile) {
		service.update(itemDesignFile);
		return Result.OK(itemDesignFile);
	}

	@DeleteMapping("remove/{id}")
	@Operation(summary = "删除产品工艺路线明细(工序)工艺图纸", description = "删除产品工艺路线明细(工序)工艺图纸", method = "DELETE")
	public Result<ProdTecRoutProcTechDrawing> remove(@PathVariable(name = "id", required = true) String id ) {
		service.delete(ProdTecRoutProcTechDrawing.class, id);
		return Result.OK();
	}

	@GetMapping("sign_out/{id}")
	@Operation(summary = "签出产品工艺路线明细(工序)工艺图纸", description = "删除产品工艺路线明细(工序)工艺图纸", method = "GET")
	public Result<ProdTecRoutProcTechDrawing> signOut(@PathVariable(name = "id", required = true) String id ) {
		ProdTecRoutProcTechDrawing file = service.getById(ProdTecRoutProcTechDrawing.class, id);
		if (file==null){
			return Result.error("未匹配到数据");
		}
		if (!file.getSignOffFlag().equals(CommonEnums.YesNo.NO.getCode())){
			return Result.error("仅未签出操作状态下允许签出操作");
		}
		file.setSignOffFlag(CommonEnums.YesNo.YES.getCode());
		file.setSignOffer(SecurityUtils.getLoginUsername());
		file.setSignOffTime(new Date());
		service.update(file);
		return Result.OK();
	}

	@GetMapping("sign_out_cancle/{id}")
	@Operation(summary = "签出产品工艺路线明细(工序)工艺图纸-取消", description = "删除产品工艺路线明细(工序)工艺图纸-取消", method = "GET")
	public Result<ProdTecRoutProcTechDrawing> signOutCancle(@PathVariable(name = "id", required = true) String id ) {
		return service.signOutCancle(id);
	}
	@PutMapping("sign_in_apply")
	@Operation(summary = "签入申请", description = "签入申请", method = "PUT")
	public Result<ProdTecRoutProcTechDrawingSignIn> signInApply(@Parameter(description = "签入数据,传惨字段:drawingFileId,fileNo,fileName,length,remarks,extension,signInDesc") @RequestBody ProdTecRoutProcTechDrawingSignIn signIn) {
		return service.signInApply(signIn);
	}

}
